import csv
import matplotlib.pyplot as plt # 或者：from matplotlib import pyplot as plt
from datetime import datetime


#【读取文件】获取最高气温、获取日期
# file_name = ".\\data\\sitka_weather_07-2018_simple.csv"
# file_name = ".\\data\\sitka_weather_2018_simple.csv"
file_name = ".\\data\\death_valley_2018_simple.csv"


with open(file_name) as data_file:
    reader = csv.reader(data_file)
    header_row = next(reader)
    # print(header_row)

    for index, column_header in enumerate(header_row):
        print(index, column_header)
        # 0 STATION
        # 1 NAME
        # 2 DATE
        # 3 PRCP
        # 4 TAVG
        # 5 TMAX
        # 6 TMIN

    dates = []
    highs, lows = [], []
    for row in reader:
        try:
            current_date = datetime.strptime(row[2], "%Y-%m-%d")
            high = int(row[5])  # 最高温度在第5列（TMAX），并且要将字符格式转换为整型格式
            low = int(row[6])   # 最低温度在第6列（TMIX），并且要将字符格式转换为整型格式
        except ValueError:
            print(current_date, "Missing data!")
        else:
            dates.append(current_date)
            highs.append(high)
            lows.append(low)

    print(highs)


#【绘制图形】
fig = plt.figure(figsize=(2560/300, 1920/300), dpi=300)
plt.plot(dates, highs, c="red", linewidth=0.5, alpha=0.8)  # 最高温度
plt.plot(dates, lows, c="blue", linewidth=0.5, alpha=0.8)  # 最低温度
plt.fill_between(dates, highs, lows, facecolor="blue", alpha=0.1)  # 填充

plt.title("Daily high temperatures (2018)", fontsize=14)
fig.autofmt_xdate()     # 绘制斜的日期标签，以免它们彼此重叠
plt.xlabel("", fontsize=12)
plt.ylabel("Temperature (F)", fontsize=12)
plt.tick_params(axis="both", which="major", labelsize=10)

plt.savefig("Figure.png")

plt.show()